import EventEmitter from 'events'

export class TextModel extends EventEmitter {
  spacers_ = ""
  constructor() {
    super()
  }
  // 文本发生变化
  textChangeEmit_() {
    this.emit('text-change')
    console.log(this.spacers_, 'this.spaveers')
  }
  insert(spacerIndex: number, spacers: string) {
    const oldSpacers = this.spacers_
    this.spacers_ = oldSpacers.slice(0, spacerIndex) + spacers + oldSpacers.slice(spacerIndex)
    this.textChangeEmit_()
  }
  delete(startIndex: number, endIndex: number) {
    if (startIndex > endIndex) {
      [endIndex, startIndex] = [startIndex, endIndex]
    }
    const oldSpacers = this.spacers_
    this.spacers_ = oldSpacers.slice(0, startIndex) + oldSpacers.slice(endIndex)
    this.textChangeEmit_()
  }
  getSpacers() {
    return this.spacers_
  }
}


export default TextModel